package Translate;

import java.util.List;


import Absyn.MiniJava.Stm;
import Translate.Temp.Label;
import Translate.Tree.Stm.SEQ;


public class TreeNode {
	public TreeNode rightChild;
	public TreeNode leftChild;
	public int myValue;
	public Label testLabel;
	public Label BodyLabel;
	public Label nextJump;
	public int myIndex;
	public List<Stm> statements;
	public Translate.Tree.Exp valueExpression;
	public boolean amILast = false;
	
	
	boolean amIdefault = false;
	public Tree.Stm resolve(Absyn.MiniJava.Visitor<Void, Exp> vistor, Void d,TreeNode defaultNode,Label currentExit,
			Translate.Tree.Exp switchExpression){
		Tree.Stm stat;
		if(!amIdefault){
			stat = MiniJava.LABEL(testLabel);
			Label lowerTest = new Label();
			if(leftChild!=null){
				System.out.println(">>>>>>>>case1");
				stat = MiniJava.SEQ(stat,new Tree.Stm.CJUMP(Tree.Stm.CJUMP.Operator.BGT,switchExpression,valueExpression,leftChild.testLabel,lowerTest));
			}
			else{
				if(defaultNode!=null)
				{
					System.out.println(">>>>>>>>case2");
					stat = MiniJava.SEQ(stat,new Tree.Stm.CJUMP(Tree.Stm.CJUMP.Operator.BGT,switchExpression,valueExpression,defaultNode.BodyLabel,lowerTest));
				}else{
					System.out.println(">>>>>>>>case3");
					stat = MiniJava.SEQ(stat,new Tree.Stm.CJUMP(Tree.Stm.CJUMP.Operator.BGT,switchExpression,valueExpression,currentExit,lowerTest));
				}
			}
			stat = MiniJava.SEQ(stat,MiniJava.LABEL(lowerTest));
			if(rightChild!=null){
				System.out.println(">>>>>>>>case4");
				stat = MiniJava.SEQ(stat,new Tree.Stm.CJUMP(Tree.Stm.CJUMP.Operator.BLT,switchExpression,valueExpression,rightChild.testLabel,BodyLabel));
			}
			else{
				if(defaultNode!=null){
					System.out.println(">>>>>>>>case5");
					stat = MiniJava.SEQ(stat,new Tree.Stm.CJUMP(Tree.Stm.CJUMP.Operator.BLT,switchExpression,valueExpression,defaultNode.BodyLabel,BodyLabel));
				}
				else{
					System.out.println(">>>>>>>>case6");
					stat = MiniJava.SEQ(stat,new Tree.Stm.CJUMP(Tree.Stm.CJUMP.Operator.BLT,switchExpression,valueExpression,currentExit,BodyLabel));
				}
			}
			stat = MiniJava.SEQ(stat,MiniJava.LABEL(BodyLabel));
		}
		else{
			stat = MiniJava.SEQ(MiniJava.LABEL(BodyLabel));
		}
		for(Stm s:statements){
			stat = MiniJava.SEQ(stat,s.accept(vistor, d).unNx());
		}
		if(nextJump!=null)
			stat = MiniJava.SEQ(stat,MiniJava.JUMP(nextJump));
		return stat;
	}

	public TreeNode(){
		BodyLabel = new Label();
		testLabel = new Label();
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
